import { Injectable } from '@nestjs/common';
import { resolve } from 'path';

@Injectable()
export class ApSystemService {
  async baseband_ctl(body: any) {
    await new Promise((resolve) => {
      setTimeout(() => {
        resolve(true);
      }, 2000);
    });
    return {
      code: 200,
      message: 'success',
      data: {
        message: '\r\n+CREGXW: 0,0\r\n\r\noK\r\n\r\n',
      },
    };
  }

  private info = [
    {
      id: 0,
      name: '基带1',
      status: 'success',
      ip: '192.168.1.1',
      bool: true,
      devinfo: {
        config: 'lowrail',
        version: 'Device not connected',
        board_temer_temper: 80,
      },
    },
    {
      id: 1,
      name: '基带2',
      status: 'success',
      ip: '192.168.1.2',
      bool: false,
      devinfo: {
        config: 'lowrail',
        version: 'Device not connected',
        board_temer_temper: 80,
      },
    },
  ];

  async baseband_get() {
    return {
      code: 200,
      message: 'success',
      data: {
        info: this.info,
      },
    };
  }

  async basebandSwitch(id: any) {
    const index = this.info.findIndex((item) => item.id === id);
    this.info.forEach((item) => {
      item.bool = false;
    });
    this.info[index].bool = true;
    return {
      code: 200,
      message: 'success',
    };
  }

  public quickList = [];
  public quickId = 0;
  async saveQuick(body: any) {
    const index = this.quickList.findIndex((item) => item.id === body.id);
    if (index === -1) {
      this.quickList.push({
        ...body,
        id: this.quickId,
      });
      this.quickId++;
    } else {
      this.quickList[index] = body;
    }
    return {
      code: 200,
      message: 'success',
    };
  }

  async getQuick(body: any) {
    return {
      code: 200,
      message: 'success',
      data: {
        quickList: this.quickList,
      },
    };
  }

  async deleteQuick(body: any) {
    const index = this.quickList.findIndex((item) => item.id === body.id);
    this.quickList.splice(index, 1);
    return {
      code: 200,
      message: 'success',
    };
  }

  public apList: any[] = [
    {
      index: 0,
      version: 'string',
    },
    {
      index: 1,
      version: 'string',
    },
    {
      index: 2,
      version: 'string',
    },
  ];
  public currentAp: any = this.apList[0];
  async getApVersion() {
    return {
      code: 200,
      message: 'success',
      data: this.currentAp,
    };
  }

  async getApList() {
    console.log('11111');
    await new Promise((resolve, reject) => {
      setTimeout(resolve, 2000);
    });
    return {
      code: 200,
      message: 'success',
      data: {
        list: this.apList,
      },
    };
  }

  async setApVersion(index: any) {
    const item = this.apList.find((item) => item.index === index);
    this.currentAp = item;
    return {
      code: 200,
      message: 'success',
    };
  }

  async deleteApVersion(index: any) {
    const item = this.apList.find((item) => item.index === index);
    this.apList.splice(item.index, 1);
    this.currentAp = this.apList[0];
    return {
      code: 200,
      message: 'success',
    };
  }

  public cpFirmwareList: any[] = [
    {
      name: 'cp1',
      timestamp: Math.floor(new Date().getTime() / 1000),
    },
    {
      name: 'cp2',
      timestamp: Math.floor(new Date().getTime() / 1000),
    },
    {
      name: 'cp3',
      timestamp: Math.floor(new Date().getTime() / 1000),
    },
  ];

  public currentCpFirmware: any = this.cpFirmwareList[0];

  async getCpFirmwareList() {
    return {
      code: 200,
      message: 'success',
      data: {
        list: this.cpFirmwareList,
      },
    };
  }

  async setCpFirmware(name: any) {
    const item = this.cpFirmwareList.find((item) => item.name === name);
    this.currentCpFirmware = item;
    return {
      code: 200,
      message: 'success',
    };
  }

  async getCpFirmware() {
    return {
      code: 200,
      message: 'success',
      data: {
        name: this.currentCpFirmware.name,
      },
    };
  }

  async deleteCpFirmware(name: any) {
    const index = this.cpFirmwareList.findIndex((item) => item.name === name);
    this.cpFirmwareList.splice(index, 1);
    this.currentCpFirmware = this.cpFirmwareList[0];
    return {
      code: 200,
      message: 'success',
    };
  }

  private cpDeviceInfo = {
    info: [
      {
        name: 'heap1',
        total: '8388608',
        avail: '3128464',
      },
      {
        name: 'mempoo1',
        total: '8388608',
        avail: '128264',
        block: '20',
      },
      {
        name: 'mempoo2',
        total: '8388208',
        avail: '128264',
        block: '20',
      },
      {
        name: 'mempoo3',
        total: '8388308',
        avail: '5128434',
        block: '32',
      },
      {
        name: 'mempoo4',
        total: '8388208',
        avail: '128264',
        block: '20',
      },
      {
        name: 'mempoo5',
        total: '8388208',
        avail: '128264',
        block: '20',
      },
      {
        name: 'mempoo6',
        total: '8388208',
        avail: '128264',
        block: '20',
      },
      {
        name: 'mempoo7',
        total: '8388208',
        avail: '128264',
        block: '20',
      },
      
      
    ],
  };

  async getCpDeviceInfo() {
    return {
      code: 200,
      message: 'success',
      data: this.cpDeviceInfo,
    };
  }

  private basebandInsertionLossList = [
    {
      id:1,
      value:2,
    },
    {
      id:2,
      value:3,
    },
  ]

  async getBasebandInsertionLoss() {
    return {
      code: 200,
      message: 'success',
      data: {
        list: this.basebandInsertionLossList,
      },
    };
  }

  async setBasebandInsertionLoss(body: any) {
    this.basebandInsertionLossList = body.list;
    return {
      code: 200,
      message: 'success',
    };
  }

  public curCpType  = [0,1,2,3]

  async getCpType() {
    return {
      code: 200,
      message: 'success',
      data: {
        cur_cp_type: 2,
      },
    };
  }

  async getCumxInfo() {
    return {
      code: 200,
      message: 'success',
      data: {
        cmux_status: this.curCpType[Math.floor(Math.random() * this.curCpType.length)] % 2 === 0 ? "on" : 'off'
      },
    };
  }

  public isSwitchLeoGeoAuto = 0
  async getSwitchLeoGeo() {
    return {
      code: 200,
      message: 'success',
      data: {
        // 随机生成0或1
        is_switch_leo_geo_auto: this.isSwitchLeoGeoAuto,
      },
    };
  }

  async setSwitchLeoGeo(is_switch_leo_geo_auto: any) {
    this.isSwitchLeoGeoAuto = is_switch_leo_geo_auto;
    return {
      code: 200,
      message: 'success',
    };
  }

  }
